package com.xindacloud.sms.adapter.service.sms.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.nexmo.client.sms.SearchSmsResponse;
import com.nexmo.client.sms.SmsClient;
import com.xindacloud.sms.adapter.constant.CommonConstant;
import com.xindacloud.sms.adapter.dto.SearchMessageDTO;
import com.xindacloud.sms.adapter.entity.SmsAgent;
import com.xindacloud.sms.adapter.service.factory.NexmoClientFactory;
import com.xindacloud.sms.adapter.service.sms.ISubmitMessageService;
import com.xindacloud.sms.adapter.utils.Base64DecodeHelp;
import com.xindacloud.sms.adapter.vo.SearchNexmoMessageVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Deprecated
@Service("nexmoSmsSearchService")
@Slf4j
public class NexmoSmsSearchServiceImpl implements ISubmitMessageService<SearchMessageDTO, SearchNexmoMessageVO> {

    @Autowired
    NexmoClientFactory nexmoClientFactory;


    @Override
    public SearchNexmoMessageVO searchSms(SearchMessageDTO searchMessageDTO, SmsAgent smsAgent) {

        List<String> messageIds = searchMessageDTO.getMessageIds();

        SearchSmsResponse searchSmsResponse = null;
        String searchDate = searchMessageDTO.getStartDate();
        String searchTo = searchMessageDTO.getTo();
        SmsClient smsClient = nexmoClientFactory
                .getNexmoClient(Base64DecodeHelp.decode(smsAgent.getApiKey()),
                        Base64DecodeHelp.decode(smsAgent.getApiSecret()), smsAgent.getApiUrl())
                .getSmsClient();
        if (StringUtils.isEmpty(searchDate)
                && StringUtils.isEmpty(searchTo)
                && CollectionUtils.isNotEmpty(messageIds)) {
            String joinIds = String.join(CommonConstant.SEPARATOR_STR, messageIds);
            searchSmsResponse = smsClient.searchMessages(messageIds.get(0), joinIds);
        }
        if (StringUtils.isNotEmpty(searchDate)
                && StringUtils.isNotEmpty(searchTo)) {


            Date date = null;
            try {
                date = DateUtils.parseDate(searchDate, "yyyy-MM-dd");
            } catch (ParseException e) {
                log.error(e.getMessage(), e);
            }
            searchSmsResponse = smsClient.searchMessages(date, searchTo);
        }

        if (Objects.isNull(searchSmsResponse)) {
            return SearchNexmoMessageVO.builder().build();
        }
        SearchNexmoMessageVO.SearchNexmoMessageVOBuilder builder = SearchNexmoMessageVO.builder();
        List<SearchNexmoMessageVO.MessageInfo> messageInfoList = Arrays.stream(searchSmsResponse.getItems())
                .map(rs -> {
                    SearchNexmoMessageVO.MessageInfo messageInfo = new SearchNexmoMessageVO.MessageInfo();
                    BeanUtils.copyProperties(rs, messageInfo);
                    messageInfo.setSendTo(rs.getTo());
                    messageInfo.setSendFrom(rs.getFrom());
                    messageInfo.setMessageBody(rs.getBody());
                    messageInfo.setDateReceived(rs.getDateReceived());
                    return messageInfo;
                }).collect(Collectors.toList());

        builder.list(messageInfoList);
        return builder.build();
    }
}
